package models;

import play.modules.siena.EnhancedModel;
import siena.Generator;
import siena.Id;
import controllers.Registration;
import controllers.etc.BCrypt;

public class User extends EnhancedModel {

	@Id(Generator.AUTO_INCREMENT)
	public Long id;

	public String email;

	private String pwHash;

	public String confirmationCode;

	public String recoverPasswordCode;

	public User(String email, String password) {
		if (email == null || email.isEmpty())
			throw new RuntimeException("User must have an email");
		if (password == null || email.isEmpty())
			throw new RuntimeException("User must have a password");
		this.email = email;
		int saltFactor = Integer.parseInt(play.Play.configuration.getProperty(
				"registration.salt_factor", "10"));
		this.pwHash = BCrypt.hashpw(password, BCrypt.gensalt(saltFactor));
		this.confirmationCode = Registration.shortUUID();
	}

	public void setPasswordHash(String password) {

		int saltFactor = Integer.parseInt(play.Play.configuration.getProperty(
				"registration.salt_factor", "10"));
		this.pwHash = BCrypt.hashpw(password, BCrypt.gensalt(saltFactor));

	}

	public boolean isThisCorrectUserPassword(String plainTextPassword) {

		return BCrypt.checkpw(plainTextPassword, pwHash);

	}

}